package com.drink.yan.svc.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.drink.yan.core.mapper.YanTaskDealMapper;
import com.drink.yan.core.mapper.YanTaskMapper;
import com.drink.yan.intf.define.*;
import com.drink.yan.intf.entity.YanCommission;
import com.drink.yan.intf.entity.YanTask;
import com.drink.yan.intf.entity.YanTaskDeal;
import com.drink.yan.intf.vo.*;
import com.drink.yan.web.rest.req.cmd.ApproveTaskCmd;
import com.drink.yan.web.rest.req.cmd.CancelTaskCmd;
import com.drink.yan.web.rest.req.cmd.SaveCustomerTaskCmd;
import com.drink.yan.web.rest.req.qry.PageTaskQry;
import com.meta.act.app.service.ISysDictTypeService;
import com.meta.framework.common.utils.SecurityUtils;
import org.apache.tools.ant.Task;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
 * java类简单作用描述
 *
 * @Title:
 * @Package: com.drink.yan.svc.service.
 * @Author: M.simple
 * @Remark: The modified content
 * @CreateDate: 2023/11/24 20:44
 * @Version: v2.0
 */
@Service
public class YanTaskService extends ServiceImpl<YanTaskMapper, YanTask> {

	@Resource
	private YanTaskDealMapper yanTaskDealMapper;

	@Resource
	private YanCommissionService yanCommissionService;

	@Resource
	private ISysDictTypeService iSysDictTypeService;

	public List<YanTaskDealVo> listMyDealTask(PageTaskQry qry) {

		List<YanTaskDealVo> yanTaskDealVos = this.baseMapper.listMyDealTask(qry);
		Map<String, String> map = iSysDictTypeService.selectDictMapByType("task_deal_status");
		yanTaskDealVos.forEach(x -> {
			x.setDealImages(JSONObject.parseArray(x.getDealImagesStr(), String.class));
			x.setDealStatusName(map.get(x.getDealStatus()));
		});
		return yanTaskDealVos;
	}

	public List<YanTaskDealVo> listDealTask(PageTaskQry qry) {

		List<YanTaskDealVo> yanTaskDealVos = this.baseMapper.listDealTask(qry);
		Map<String, String> map = iSysDictTypeService.selectDictMapByType("task_deal_status");
		yanTaskDealVos.forEach(x -> {
			x.setDealImages(JSONObject.parseArray(x.getDealImagesStr(), String.class));
			x.setDealStatusName(map.get(x.getDealStatus()));
		});
		return yanTaskDealVos;
	}

	public List<YanTask> listNeedReceiveTask(PageTaskQry qry) {

		return this.baseMapper.listNeedReceiveTask(qry);
	}

	public CustomerTaskSummaryVo customerTaskSummary(PageTaskQry qry) {

		CustomerTaskSummaryVo customerTaskSummaryVo = new CustomerTaskSummaryVo();

		List<YanTask> needCompleteNum = this.baseMapper.listNeedReceiveTask(qry);
		customerTaskSummaryVo.setNeedCompleteNum(needCompleteNum.size());
		qry.setDealStatus(YanTaskDealStatusE.checking.getCode());
		Integer checkingNum = this.baseMapper.countMyDealTask(qry);
		customerTaskSummaryVo.setCheckingNum(checkingNum);
		qry.setDealStatus(YanTaskDealStatusE.check_succ.getCode());
		Integer succNum = this.baseMapper.countMyDealTask(qry);
		customerTaskSummaryVo.setCheckSuccNum(succNum);
		qry.setDealStatus(YanTaskDealStatusE.check_fail.getCode());
		Integer failNum = this.baseMapper.countMyDealTask(qry);
		customerTaskSummaryVo.setCheckFailNum(failNum);

		return customerTaskSummaryVo;
	}

	public List<TaskVo> listTask(PageTaskQry qry) {

		List<TaskVo> taskVos = this.baseMapper.listTask(qry);
		Map<String, String> map = iSysDictTypeService.selectDictMapByType("task_publish_status");

		taskVos.forEach(x -> {
			x.setTaskImages(JSONObject.parseArray(x.getTaskImagesStr(), String.class));
			x.setTaskStatusName(map.get(x.getTaskStatus()));
		});
		return taskVos;
	}

	public YanTaskDeal getYanTaskDeal(Long taskNo) {
		LambdaQueryWrapper<YanTaskDeal> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(YanTaskDeal::getTaskNo, taskNo);
		return this.yanTaskDealMapper.selectOne(queryWrapper);
	}

	public YanTask saveTask(YanTask cmd) {

		cmd = saveTask(cmd, true);

		return cmd;
	}

	public YanTask saveTask(YanTask cmd, boolean loginFlag) {
		if (Objects.isNull(cmd.getTaskNo())) {
			cmd.setCreateBy(SecurityUtils.getUserId());
			cmd.setTaskStatus(YanTaskStatusE.ready.getCode());
			this.baseMapper.insert(cmd);
		} else {
			YanTask task = getYanTask(cmd.getTaskNo());
			Optional.ofNullable(cmd.getTaskName()).ifPresent(task::setTaskName);
			Optional.ofNullable(cmd.getTaskStatus()).ifPresent(task::setTaskStatus);
			Optional.ofNullable(cmd.getTaskContent()).ifPresent(task::setTaskContent);
			Optional.ofNullable(cmd.getBeginTime()).ifPresent(task::setBeginTime);
			Optional.ofNullable(cmd.getEndTime()).ifPresent(task::setEndTime);
			Optional.ofNullable(cmd.getTaskImages()).ifPresent(task::setTaskImages);
			Optional.ofNullable(cmd.getTaskPrice()).ifPresent(task::setTaskPrice);

			if(loginFlag){
				task.setUpdateBy(SecurityUtils.getUserId());
			}


			this.baseMapper.updateById(task);
			cmd = task;
		}
		return cmd;
	}

	private YanTask getYanTask(Long taskNo) {
		return this.baseMapper.selectById(taskNo);
	}

	public YanTaskDeal checkSucc(ApproveTaskCmd cmd) {

		YanTaskDeal yanTaskDeal = this.yanTaskDealMapper.selectById(cmd.getDealId());
		yanTaskDeal.setDealStatus(YanTaskDealStatusE.check_succ.getCode());

		this.yanTaskDealMapper.updateById(yanTaskDeal);

		YanTask yanTask = getYanTask(yanTaskDeal.getTaskNo());

		YanCommission yanCommission = new YanCommission();
		yanCommission.setBusinessKey(yanTaskDeal.getTaskNo());
		yanCommission.setBusinessSubKey(yanTaskDeal.getId());
		yanCommission.setUserId(yanTaskDeal.getDealUid());
		yanCommission.setCommissionType(YanCommissionTypeE.promotion_task.getCode());
		yanCommission.setCommissionStatus(YanCommissionStatusE.in_account.getCode());
		yanCommission.setCommissionTypeName(YanCommissionTypeE.promotion_task.getRemark());
		yanCommission.setCommissionName(yanTask.getTaskName());
		yanCommission.setBusinessAmount(yanTask.getTaskPrice());
		yanCommission.setUserType(YanUserTypeE.agency.getCode());
		yanCommissionService.generateCommission(yanCommission);

		return yanTaskDeal;

	}

	public YanTaskDeal checkFail(ApproveTaskCmd cmd) {

		YanTaskDeal yanTaskDeal = this.yanTaskDealMapper.selectById(cmd.getDealId());
		yanTaskDeal.setDealStatus(YanTaskDealStatusE.check_fail.getCode());
		yanTaskDeal.setRemark(cmd.getRemark());

		this.yanTaskDealMapper.updateById(yanTaskDeal);

		return yanTaskDeal;
	}

	public YanTaskDeal saveCustomerTask(SaveCustomerTaskCmd cmd) {

		Long taskNo;
		if (Objects.isNull(cmd.getTaskNo())) {

			YanTask yanTask = new YanTask();
			yanTask.setTaskName(cmd.getTaskTypeName());
			yanTask.setTaskContent(cmd.getTaskContent());
			yanTask.setTaskType(cmd.getTaskType());
			yanTask.setTaskStatus(null);
			yanTask.setTaskCate("private");
			yanTask.setTaskImages(cmd.getTaskImages());
			yanTask.setCreateBy(cmd.getUserId());
			this.baseMapper.insert(yanTask);

			YanTaskDeal yanTaskDeal = new YanTaskDeal();
			yanTaskDeal.setTaskNo(yanTask.getTaskNo());
			yanTaskDeal.setDealUid(cmd.getUserId());
			yanTaskDeal.setDealImages(cmd.getTaskImages());
			yanTaskDeal.setDealContent(cmd.getTaskContent());
			yanTaskDeal.setDealStatus(YanTaskDealStatusE.checking.getCode());
			yanTaskDeal.setDealTime(LocalDateTime.now());
			yanTaskDeal.setCreateBy(cmd.getUserId());
			this.yanTaskDealMapper.insert(yanTaskDeal);
			taskNo = yanTask.getTaskNo();
		} else {

			YanTaskDeal yanTaskDeal = getYanTaskDeal(cmd.getTaskNo());
			if (Objects.isNull(yanTaskDeal)) {
				yanTaskDeal = new YanTaskDeal();
				yanTaskDeal.setTaskNo(cmd.getTaskNo());
				yanTaskDeal.setDealUid(cmd.getUserId());
				yanTaskDeal.setDealImages(cmd.getTaskImages());
				yanTaskDeal.setDealContent(cmd.getTaskContent());
				yanTaskDeal.setDealStatus(YanTaskDealStatusE.checking.getCode());
				yanTaskDeal.setDealTime(LocalDateTime.now());
				yanTaskDeal.setCreateBy(cmd.getUserId());
				this.yanTaskDealMapper.insert(yanTaskDeal);
			} else {
				yanTaskDeal.setDealContent(cmd.getTaskContent());
				yanTaskDeal.setDealImages(cmd.getTaskImages());
				this.yanTaskDealMapper.updateById(yanTaskDeal);
			}

			YanTask yanTask = getYanTask(cmd.getTaskNo());
			yanTask.setTaskContent(cmd.getTaskContent());
			this.yanTaskDealMapper.updateById(yanTaskDeal);
			taskNo = yanTask.getTaskNo();
		}
		return getYanTaskDeal(taskNo);
	}

	public TaskVo getDetail(Long taskNo) {

		YanTask yanTask = getYanTask(taskNo);
		if (Objects.isNull(yanTask)) {
			return null;
		}

		return yanTask.transform(TaskVo.class);
	}

	public YanTask cancelTask(CancelTaskCmd cmd) {
		YanTask yanTask = getYanTask(cmd.getTaskNo());

		if (YanTaskStatusE.ready.getCode().equals(yanTask.getTaskStatus())) {
			yanTask.setTaskStatus(YanTaskStatusE.cancelled.getCode());
		}

		this.baseMapper.updateById(yanTask);

		return yanTask;
	}

}
